package me.minicoral.dao.impl;

import java.sql.SQLException;
import java.util.List;

import me.minicoral.action.Blogger;
import me.minicoral.dao.BloggerDao;
import me.minicoral.exception.DaoException;
import me.minicoral.util.DBCPUtil;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

public class BloggerDaoImpl implements BloggerDao
{

private QueryRunner qr = new QueryRunner(DBCPUtil.getDataSource());
	
	@Override
	public List<Blogger> findAllBloggers(String userId)
	{
		try
        {
            String sql = "select bid,tblogger.uuid,bblog,bclicksum,bpraisesum,bpubtime,unick from tfriends,tuser,tblogger where cuuid=tblogger.uuid and tblogger.uuid=tuser.uuid and fuuid=?";
            Object prams = userId;
            return qr.query(sql,new BeanListHandler<Blogger>(Blogger.class),prams);
        }
        catch (SQLException e)
        {
            throw new DaoException(e);
        }
	}

	@Override
	public int addBlogger(Blogger blogger)
	{
		try
		{
			String sql = "insert into tblogger(bid,uuid,bblog,bclicksum,bpraisesum,bpubtime) values(?,?,?,?,?,SYSDATE)";
			Object prams[] = {blogger.getBid(),blogger.getUuid(),blogger.getBblog(),blogger.getBclicksum(),blogger.getBpraisesum()};
			return qr.update(sql,prams);
		}
		catch (SQLException e)
		{
			throw new DaoException(e);
		}
	}
	
}	
